package com.cuit.controller.book;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.mapper.Book.CommentMapper;
import com.cuit.mapper.UserMapper;
import com.cuit.pojo.Comment;
import com.cuit.pojo.User;
import com.cuit.result.R;
import com.cuit.service.Book.TimeCha;
import com.cuit.service.CommentService;
import com.cuit.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Api(tags = "评论管理")
@Controller
@RequestMapping("/comment")
public class commentController {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private FileService  fileService;
    @Resource
    private TimeCha timeCha;
    @Resource
    private CommentService commentService;

    @PostMapping("/setComment")
    @ResponseBody
    @ApiOperation("添加评论")
    public R setComment(@RequestBody String comment){
        String coms = comment.substring(8, comment.length() - 2);
        String[] info = coms.split(",");
        Comment com=new Comment();
        User user = userMapper.selectById(info[1]);
        com.setUserName(user.getUsername());
        com.setBookId(Integer.valueOf(info[0]));
        com.setUserId(Integer.valueOf(info[1]));
        if(info[2].equals("")){
            return  R.ok();
        }
        com.setComDesc(info[2]);
        com.setCreateTime(new Date());
        commentMapper.insert(com);
        return  R.ok();
    }
    @GetMapping("/selComCount/{bId}")
    @ResponseBody
    @ApiOperation("该商品的评论条数")
    public  R selComCount(@PathVariable String bId){
        QueryWrapper<Comment> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("bookId",bId);
        Integer countCom = commentMapper.selectCount(queryWrapper);
        return R.ok().data("countCom",countCom);
    }
    @GetMapping("/getBookCom/{bId}")
    @ResponseBody
    @ApiOperation("该商品的评论信息")
    public  R getBookCom(@PathVariable String bId){
        String format = "yyyy-MM-dd HH:mm:ss";
        QueryWrapper<Comment> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("bookId",bId);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String nowTime = simpleDateFormat.format(System.currentTimeMillis());
        comments.forEach(com->{
            User user = userMapper.selectById(com.getUserId());
            com.setState(fileService.allPath(user.getAvatar()));
            String timeC = timeCha.countTimeCha(simpleDateFormat.format(com.getCreateTime()), nowTime, format);
            com.setTest(timeC);
        });
        return R.ok().data("comDetail",comments);
    }
    @GetMapping("/delCom/{Id}")
    @ResponseBody
    @ApiOperation("三处评论")
    public  R delComCount(@PathVariable String Id){
        commentMapper.deleteById(Id);
        return R.ok();
    }
    //后台管理评论
    //查询
    @GetMapping("/selectById")
    public String selectById(ModelMap model, @RequestParam(value = "pn", defaultValue = "1") Integer pn)   {
        Page<Comment> commentPage=new Page<>(pn,6);
        Page<Comment> page=commentService.page(commentPage,null);
        model.addAttribute("page",page);
        return "admin/widgets-comment";
    }

    //搜索
    @GetMapping("/select")
    public String select(ModelMap model,@RequestParam String username,@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("username",username);
        Page<Comment> commentPage=new Page<>(pn,6);
        Page<Comment> page=commentService.page(commentPage,queryWrapper);
        model.addAttribute("page",page);

        System.out.println("来了");
        return "admin/widgets-comment";
    }

    //删除
    @RequestMapping("/deleteComment/{id}")
    public String deleteComment(@PathVariable Long id, RedirectAttributes attributes){
        int delete=commentMapper.deleteById(id);
        if(delete!=0){
            attributes.addFlashAttribute("message","删除用户成功");
            return "redirect:/comment/selectById";
        }else{
            attributes.addFlashAttribute("message","删除用户失败");
            return "redirect:/comment/selectById";
        }
    }

    //修改
    @GetMapping("/updateComment/{id}")
    public String updateComment(@PathVariable Integer id,ModelMap model){
        model.addAttribute("comment",commentMapper.selectById(id));
        return "admin/widgets-commentUpdate";
    }

    @RequestMapping("/edit")
    public String edit(Comment comment){
        boolean b=commentService.updateById(comment);
        System.out.println("来了0000");
        return "redirect:/comment/selectById";
    }


    //分页查询
    @RequestMapping("/selectFY")
    @ResponseBody
    public R selectFY(){
        IPage<Comment> pageParam=new Page<>(1,5);
        commentMapper.selectPage(pageParam,null);
        List<Comment> records=pageParam.getRecords();
        if(records!=null){
            System.out.println("当前页:"+pageParam.getCurrent());
            System.out.println("每页数据内容："+pageParam.getRecords());
            System.out.println("每页显示记录:"+pageParam.getSize());
            System.out.println("总记录数:"+pageParam.getTotal());
            System.out.println("总页数:"+pageParam.getPages());
            System.out.println("是否有下一页:"+((Page<Comment>) pageParam).hasNext());
            System.out.println("是否有上一页:"+((Page<Comment>) pageParam).hasPrevious());
            return R.ok().data("records",records);
        }else{
            return R.error();
        }
    }
}
